package component

import (
	"github.com/hyperway/pkg/helper"
)

var NamedSerializers = make(map[string]Serializer, 2)

func RegisterSerializer(typeName string, serializer Serializer) {
	helper.IsEmptyPanic(typeName, "<type-name> is empty")
	helper.IsNotNil(serializer, "<serializer> is nil")
	NamedSerializers[typeName] = serializer
}

// Serializer 序列化接口
type Serializer interface {
	// ToBytes 将对象序列化为字节数组
	ToBytes(any interface{}) (bytes []byte, err error)

	// ToObject 将字节数组反序列化为对象；
	// 注意：对象为指针类型；
	ToObject(bytes []byte, obj interface{}) error
}
